package com.example.mannortycoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mannortycoon.dao.UserCropDao;
import com.example.mannortycoon.entity.CropEntity;
import com.example.mannortycoon.entity.UserCropEntity;
import com.example.mannortycoon.entity.UserEntity;
import com.example.mannortycoon.service.CropService;
import com.example.mannortycoon.service.UserCropService;
import com.example.mannortycoon.service.UserService;
import com.example.mannortycoon.uitl.Constant;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.UserCropVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("userCropService")
public class UserCropServiceImpl extends ServiceImpl<UserCropDao, UserCropEntity> implements UserCropService {

    private final CropService cropService;

    private final UserService userService;

    @Autowired
    public UserCropServiceImpl(CropService cropService, UserService userService) {
        this.cropService = cropService;
        this.userService = userService;
    }

    @Override
    public List<UserCropVo> getUserCropList(Long userId) {
        QueryWrapper<UserCropEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.ne("number", 0);
        List<UserCropEntity> list = this.list(wrapper);
        return list.stream().map(item -> {
            UserCropVo userCropVo = new UserCropVo();
            //获取作物信息
            CropEntity cropEntity = cropService.getById(item.getCropId());
            if (cropEntity != null) {
                BeanUtils.copyProperties(cropEntity,userCropVo);
            }
            userCropVo.setCropId(item.getCropId());
            userCropVo.setNumber(item.getNumber());
            return userCropVo;
        }).collect(Collectors.toList());
    }

    @Override
    public void saveUserCropList(Long userId, List<UserCropEntity> userCropList) {
        UpdateWrapper<UserCropEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id", userId);
        this.remove(wrapper);
        if (userCropList.size() > 0) {
            this.saveOrUpdateBatch(userCropList);
        }
    }

//    @Override
//    @Transactional
//    public R buyCrop(Long userId, UserCropEntity userBuyCropInfo) {
//        Long cropId = userBuyCropInfo.getCropId();
//        //查询作物信息
//        CropEntity cropEntity = cropService.getById(cropId);
//        if (cropEntity == null) {
//            return R.error("作物不存在");
//        }
//        //查询用户作物信息
//        QueryWrapper<UserCropEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_id", userId).eq("crop_id", cropId);
//        UserCropEntity userCropEntity = this.getOne(wrapper);
//        Integer number = userBuyCropInfo.getNumber();
//        //查询用户金额
//        UserEntity userEntity = userService.getById(userId);
//        Integer priceType = cropEntity.get();
//        Double sellPrice = cropEntity.getSellPrice();
//        Double totalPrice = sellPrice * number;
//        UpdateWrapper<UserEntity> userEntityUpdateWrapper = new UpdateWrapper<>();
//        userEntityUpdateWrapper.eq("user_id", userId);
//        //判断价格类型
//        if (priceType == Constant.PRICE_TYPE.SEED_GOLD) {
//            if (userEntity.getSeedGold() < totalPrice) {
//                return R.error("种子币不足");
//            }
//            userEntityUpdateWrapper.setSql("seed_gold = seed_gold - " + number * sellPrice);
//        } else {
//            if (userEntity.getPearl() < totalPrice) {
//                return R.error("珍珠不足");
//            }
//            userEntityUpdateWrapper.setSql("pearl = pearl - " + totalPrice);
//        }
//        //修改用户金额
//        userService.update(userEntityUpdateWrapper);
//
//        //新增或修改用户种子
//        if (userCropEntity == null) {
//            userCropEntity = new UserCropEntity();
//            userCropEntity.setCropId(cropId);
//            userCropEntity.setUserId(userId);
//            userCropEntity.setCreateTime(new Date());
//            userCropEntity.setNumber(number);
//            this.save(userCropEntity);
//        } else {
//            userCropEntity.setUpdateTime(new Date());
//            userCropEntity.setNumber(userCropEntity.getNumber() + number);
//            this.updateById(userCropEntity);
//        }
//        return R.ok("购买成功");
//    }

    @Override
    @Transactional
    public R sellCrop(Long userId, Long cropId, Integer number) {
        //查询用户该作物
        QueryWrapper<UserCropEntity> userCropEntityQueryWrapper = new QueryWrapper<>();
        userCropEntityQueryWrapper.eq("user_id", userId).eq("crop_id", cropId);
        UserCropEntity userCropEntity = this.getOne(userCropEntityQueryWrapper);
        if (userCropEntity == null || userCropEntity.getNumber() < number) {
            return R.error("用户作物不足");
        }
        //获取作物信息
        CropEntity cropEntity = cropService.getById(cropId);
        if (cropEntity == null) {
            return R.error("该作物不存在");
        }
        //添加用户种子币/珍珠
        //获取用户信息
        UserEntity userEntity = userService.getById(userId);
        if (userEntity == null) {
            return R.error("用户不存在");
        }
        userEntity.setUpdateTime(new Date());
        //添加用户种子币和珍珠
        userEntity.setSeedGold(userEntity.getSeedGold() + number * cropEntity.getSeedGold());
        userEntity.setPearl(userEntity.getPearl() + number * cropEntity.getPearl());
        userService.updateById(userEntity);
        //减少用户作物
        userCropEntity.setNumber(userCropEntity.getNumber() - number);
        this.updateById(userCropEntity);
        return R.ok();
    }

    @Override
    @Transactional
    public R give(Long userId, Long userId2, Long cropId, Integer number) {
        //判断用户是否有足够的作物
        QueryWrapper<UserCropEntity> userCropEntityQueryWrapper = new QueryWrapper<>();
        userCropEntityQueryWrapper.eq("user_id", userId).eq("crop_id", cropId);
        UserCropEntity one = this.getOne(userCropEntityQueryWrapper);
        if (one == null || one.getNumber() < number) {
            return R.error("作物不足");
        }
        //1 添加另一个用户的作物
        //1.1 查找另一个用户该作物
        QueryWrapper<UserCropEntity> userCropEntityQueryWrapper1 = new QueryWrapper<>();
        userCropEntityQueryWrapper1.eq("user_id", userId2).eq("crop_id", cropId);
        UserCropEntity one1 = this.getOne(userCropEntityQueryWrapper1);
        if (one1 == null) {
            one1 = new UserCropEntity();
            one1.setUserId(userId2);
            one1.setCropId(cropId);
            one1.setCreateTime(new Date());
            one1.setNumber(number);
            this.save(one1);
        } else {
            one1.setNumber(one1.getNumber() + number);
            this.updateById(one1);
        }
        //2.减少用户作物
        one.setNumber(one.getNumber() - number);
        this.updateById(one);
        return R.ok();
    }

    @Override
    public UserCropEntity getUserCropInfo(Long userId, Long cropId) {
        QueryWrapper<UserCropEntity> userCropEntityQueryWrapper = new QueryWrapper<>();
        userCropEntityQueryWrapper.eq("user_id",userId).eq("crop_id",cropId);
        return this.getOne(userCropEntityQueryWrapper);
    }

    @Override
    public void updateNumber(Long userId, Long cropId, Integer number) {
        UpdateWrapper<UserCropEntity> userCropEntityUpdateWrapper = new UpdateWrapper<>();
        userCropEntityUpdateWrapper.eq("user_id",userId).eq("crop_id",cropId)
                .setSql("number = number + "+number);
    }

}